package com.me.springcloud.feign.ssl;

import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.SecureRandom;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

public class TrustingSSLSocketFactory2 {

	public static SSLSocketFactory create() throws Exception {
		File certFile = new File(Constants.JKS_PATH);
		KeyStore ks = KeyStore.getInstance("JKS");
		ks.load(new FileInputStream(certFile), Constants.JKS_PASS.toCharArray());

		KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
		kmf.init(ks, Constants.JKS_PASS.toCharArray());

		TrustManager[] tm = { new MyX509TrustManager() };

		SSLContext sslContext = SSLContext.getInstance("SSL");
		sslContext.init(kmf.getKeyManagers(), tm, new SecureRandom());

		SSLSocketFactory factory = sslContext.getSocketFactory();

		return factory;
	}
}
